﻿/// Sorting an array means to arrange its elements in increasing order.
/// Write a program to sort an array. Use the "selection sort" algorithm:
/// Find the smallest element, move it at the first position, find the smallest from the rest, move it at the second position, etc.

using System;

class zad7
{
    static void Main()
    {
        int[] arr = { 5, 3, 4, 7, 6, 4, 5 };

        SelectionSort(arr);

        foreach (var item in arr)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }

    private static void SelectionSort(int[] arr, bool asc = true)
    {
        int minIndex;
        int temp;

        for (int i = 0; i < arr.Length - 1; i++)
        {
            minIndex = i;

            for (int j = i + 1; j < arr.Length; j++)
            {
                if ( CheckDirection(arr[j], arr[minIndex], asc) )
                {
                    minIndex = j;
                }
            }

            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    private static bool CheckDirection(int a, int b, bool asend = true)
    {
        if (asend)
        {
            return a < b;
        }
        else
        {
            return a > b;
        }
    }
}
